package com.service.thirddatacenter.framework.configuration;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.service.thirddatacenter.framework.interceptor.APRAuthenticationInterceptor;
import com.service.thirddatacenter.framework.interceptor.HeaderHandlerInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.io.File;
import java.util.List;

/**
 *  @desc
 */
@Component
@Configuration
public class MvcConfig extends WebMvcConfigurationSupport {


	@Autowired
	private APRAuthenticationInterceptor aprAuthenticationInterceptor;

	@Autowired
	private HeaderHandlerInterceptor handlerInterceptor;

	@Autowired
	private Environment env;

	/**
	 * 功能描述:
	 * 配置静态资源,避免静态资源请求被拦截
	 *
	 * @auther: Tt(yehuawei)
	 * @date:
	 * @param:
	 * @return:
	 */
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {

		// Swagger UI resource handlers
		registry.addResourceHandler("/**")
				.addResourceLocations("classpath:/resources/")
				.addResourceLocations("classpath:/static/")
				.addResourceLocations("classpath:/templates/")
				.addResourceLocations("classpath:/public/");
		// swagger2
		registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
		registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
		registry.addResourceHandler("docs.html").addResourceLocations("classpath:/META-INF/resources/");
		registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");

		super.addResourceHandlers(registry);
	}

	/**
	 * 允许任何请求跨域访问
	 *
	 * @param registry CorsRegistry
	 */
	@Override
	public void addCorsMappings(CorsRegistry registry) {
		registry.addMapping("/**")
				.allowedOrigins("*")
				.allowedMethods("GET", "HEAD", "POST", "PUT", "PATCH", "DELETE", "OPTIONS", "TRACE");
	}

	/**
	 * 配置拦截器
	 *
	 * @param registry 拦截器注册
	 */
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(handlerInterceptor)
				.addPathPatterns("/**")
				.excludePathPatterns("/error", "/swagger-resources/**", "/file/get", "/swagger-ui.html", "/webjars/**", "/pub-resource/**");


		registry.addInterceptor(aprAuthenticationInterceptor)
				.addPathPatterns("/**")
				.excludePathPatterns("/open/**", "/logout", "/error", "/config/**", "/swagger-resources/**", "/file/get", "/swagger-ui.html", "/doc.html", "/docs.html", "/webjars/**", "/pub-resource/**","/hPool/data/**");
		super.addInterceptors(registry);
	}

	@Bean
	public MappingJackson2HttpMessageConverter customJackson2HttpMessageConverter() {
		MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
		ObjectMapper objectMapper = new ObjectMapper();
		objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
//        objectMapper.setTimeZone(TimeZone.getTimeZone("GMT+8"));
//        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
		objectMapper.setDefaultPropertyInclusion(JsonInclude.Include.NON_NULL);
		jsonConverter.setObjectMapper(objectMapper);
		return jsonConverter;
	}

	@Override
	public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
		converters.add(customJackson2HttpMessageConverter());
		super.addDefaultHttpMessageConverters(converters);
	}
}